package com.cande.loki.exercise.javabasic;

import com.alibaba.fastjson.JSONArray;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

class ChainSupplierPay{
    BigDecimal payAmount;
    Long voucherID;

    public BigDecimal getPayAmount() {
        return payAmount;
    }

    public void setPayAmount(BigDecimal payAmount) {
        this.payAmount = payAmount;
    }

    public Long getVoucherID() {
        return voucherID;
    }

    public void setVoucherID(Long voucherID) {
        this.voucherID = voucherID;
    }
}
public class TestString {
    public static final int CYCLE_NUM_BIGGER = 10_000;
    public static final int CYCLE_NUM_LOWER = 10_000;
    public static final String str1 = "张三";
    public static final String str2 = "李四";
    public static final String str3 = "王五";
    public static final String str4 = "赵六";


    public static void main(String[] args) throws InterruptedException {


        ChainSupplierPay g = new ChainSupplierPay();g.setPayAmount(new BigDecimal(111));g.setVoucherID(1l);
        ChainSupplierPay g1 = new ChainSupplierPay();
        g1.setPayAmount(new BigDecimal(222));g1.setVoucherID(2l);
        //查询付款明细
        List<ChainSupplierPay> parDetailList = new ArrayList<>();
        parDetailList.add(g);
        parDetailList.add(g1);

        //获取每个单的付款金额
        Map<Long, BigDecimal> map1 = new HashMap<>();
        Map<Long, List<ChainSupplierPay>> payAmountMap = parDetailList.stream().collect(Collectors.groupingBy(a -> a.getVoucherID()));
        for (Long voucherID : payAmountMap.keySet()) {
            BigDecimal payAmount = BigDecimal.ZERO;
            for (ChainSupplierPay pay : payAmountMap.get(voucherID)) {
                payAmount = payAmount.add(pay.getPayAmount());
            }
            map1.put(voucherID, payAmount);
        }

        StringJoiner savedDetailIDs = new StringJoiner(",").add("a");
        System.out.println(savedDetailIDs);

    Long KK = null;
        System.out.println(String.valueOf(KK));


        String outLog = "[{\"houseName\":\"遇外滩\",\"deliveryNum\":2.00000000,\"houseID\":9106907,\"balance\":0E-8,\"goodsID\":2053263,\"goodsCode\":\"280102422\",\"standardUnit\":\"瓶\",\"goodsName\":\"沙龙,勒梅尼勒,香槟,法国\",\"goodsDesc\":\"\"},{\"houseName\":\"遇外滩\",\"deliveryNum\":1.00000000,\"houseID\":9106907,\"balance\":0E-8,\"goodsID\":2053278,\"goodsCode\":\"280102437\",\"standardUnit\":\"瓶\",\"goodsName\":\"暮光酒庄,麝香低醇甜白,皮埃蒙特,意大利(375ml)\",\"goodsDesc\":\"\"},{\"houseName\":\"遇外滩\",\"deliveryNum\":4.00000000,\"houseID\":9106907,\"balance\":0E-8,\"goodsID\":2053164,\"goodsCode\":\"280102365\",\"standardUnit\":\"瓶\",\"goodsName\":\"青岛啤酒（296ml*24瓶/件）\",\"goodsDesc\":\"\"},{\"houseName\":\"遇外滩\",\"deliveryNum\":1.00000000,\"houseID\":9106907,\"balance\":0E-8,\"goodsID\":1623265,\"goodsCode\":\"280102036\",\"standardUnit\":\"瓶\",\"goodsName\":\"比利时白熊啤酒(330ml*24瓶/件)\",\"goodsDesc\":\"330ml*24瓶/件\"},{\"houseName\":\"遇外滩\",\"deliveryNum\":1.00000000,\"houseID\":9106907,\"balance\":0E-8,\"goodsID\":2053312,\"goodsCode\":\"280102471\",\"standardUnit\":\"瓶\",\"goodsName\":\"银色高地酒庄,\\\"嘉园\\\"马瑟兰,贺兰山,宁夏,中国\",\"goodsDesc\":\"\"}]";
        JSONArray.parseArray(outLog);

        Map<Long,JSONArray> map = new HashMap<>();
        map.put(1L, JSONArray.parseArray(outLog.replace("\\\"","\\\\\\\"")));

        System.out.println(map.containsKey(null));


        StringJoiner voucherDetailIDs = new StringJoiner(",");
        System.out.println(voucherDetailIDs.length());




        String stra = ":";


        String[] str = stra.split(":");

        stra.replace(str[0] + (str.length > 1 ? ":" : ""), "");

        System.out.println(stra);
        int time = 0;
        int num = 5;

        time = executeSometime(1, num);
        System.out.println("String拼接 "+ CYCLE_NUM_BIGGER +" 次，" + num + "次平均时间：" + time + " ms");

        time = executeSometime(2, num);
        System.out.println("StringBuilder拼接 "+ CYCLE_NUM_BIGGER +" 次，" + num + "次平均时间：" + time + " ms");

        time = executeSometime(3, num);
        System.out.println("String拼接单个字符串 "+ CYCLE_NUM_LOWER +" 次，" + num + "次平均时间：" + time + " ms");

        time = executeSometime(4, num);
        System.out.println("StringBuilder拼接单个字符串 "+ CYCLE_NUM_LOWER +" 次，" + num + "次平均时间：" + time + " ms");

    }
    public static int executeSometime(int kind, int num) throws InterruptedException {
        Thread.sleep(2000);
        int sum = 0;
        for (int i = 0; i < num + 5; i++) {
            long begin = System.currentTimeMillis();

            switch (kind){
                case 1:
                    useString();
                    break;
                case 2:
                    useStringBuilder();
                    break;
                case 3:
                    useStringSpliceOneStr();
                    break;
                case 4:
                    useStringBuilderSpliceOneStr();
                    break;
                default:
                    return 0;
            }

            long end = System.currentTimeMillis();

            if(i > 5){
                sum += (end - begin);
            }
        }
        return sum / num;
    }

    /**
     * 循环内 String 拼接字符串，一次循环后销毁
     */
    public static void useString(){
        for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {
            String str = str1 + i + str2 + i + str3 + i + str4 ;
        }
    }

    /**
     * 循环内 使用 StringBuilder 拼接字符串，一次循环后销毁
     */
    public static void useStringBuilder(){
        for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {
            StringBuilder sb = new StringBuilder();
            String s = sb.append(str1).append(i).append(str2).append(i).append(str3).append(i).append(str4).toString();
        }
    }
    /**
     * 多次循环拼接成一个字符串 用 String
     * 不要用这种  多了new对象和销毁对象 把stringbuilder转为string的过程
     */
    public static void useStringSpliceOneStr (){
        String str = "";
        for (int i = 0; i < CYCLE_NUM_LOWER; i++) {
            str += str1 + str2 + str3 + str4 + i;
        }
    }

    /**
     * 多次循环拼接成一个字符串 用 StringBuilder
     */
    public static void useStringBuilderSpliceOneStr(){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CYCLE_NUM_LOWER; i++) {
            sb.append(str1).append(str2).append(str3).append(str4).append(i);
        }
    }

}
